/*
*** (C) 2008 UCMai.com ***

Subj: 自定义GridPanel对象
Auth: lingq
Date: 2008-05-23
*/
Ext.namespace("Ext.ux.grid"); 
Ext.ux.grid.GridPanel = Ext.extend(Ext.grid.GridPanel, {
	pagesize:25,
	structure : '',
	readType:'json',
	url : '',
	keyField : '',
	dataObject:null,

	needGroup:false,
	myGroupField:'',
	myGroupTextTpl:'',//{text}

	needContextMenu:false,

	selectType:'',//check

	defaultSortField : '',

	needTbar : false,
	arrTbar : [],

	needMenu : false,
	arrMenu : [],

	needPage : true,

	frame : false,

	collapsible : false,
	animCollapse : true,

	//loadMask : true, 
	loadMask: {msg:'正在加载数据，请稍候...'},
	viewConfig : {
		forceFit : true 
	},

	//private
	initComponent : function() {
		if (this.structure != '') {
			this.initStructure();
		}

		Ext.ux.grid.GridPanel.superclass.initComponent.call(this);   
	},

	// private
	initEvents : function() {
		Ext.ux.grid.GridPanel.superclass.initEvents.call(this);   
		
		if(this.readType == 'json'){
			this.getStore().load({params:{start:0,limit:this.pagesize}});
		}

		if (this.loadMask) {
			this.loadMask = new Ext.LoadMask(this.bwrap,this.loadMask); 
		}

		//this.on('sortchange',function(){
			//alert(this.getStore().getSortState().direction);
			//alert(this.getStore().getSortState().field);
		//});

	},

	// private
	initStructure : function() {
		var oCM = new Array();
		var oRecord = new Array();

		var iLenTbar = this.arrTbar.length;

		if(this.selectType=='check'){
			var sm = new Ext.grid.CheckboxSelectionModel();
			oCM[oCM.length] = sm;
			this.selModel = sm;
		}

		var len = this.structure.length;
		for (var i = 0;i < len; i++) {
			var c = this.structure[i];
			if (c.hidden == undefined || !c.hidden) {
				oCM[oCM.length] = {
					header : c.header,
					dataIndex : c.name,
					width : c.width,
					align : c.type == 'numeric' ? 'right' : 'left',
					renderer : c.renderer
				};   
			}
			oRecord[oRecord.length] = {
				name : c.name ,
				type : c.type == 'date' ? 'date' : 'string'
			}
		}   

		this.cm = new Ext.grid.ColumnModel(oCM);
		this.cm.defaultSortable = true;
 
		var record = Ext.data.Record.create(oRecord);

		var reader;
		switch (this.readType){
			case 'json':
				reader = new Ext.data.JsonReader({
					totalProperty : "totalProperty",
					root : "records" ,
					id : this.keyField
				}, record); 

				this.ds = new Ext.data.GroupingStore( {
					proxy : new Ext.data.HttpProxy( {
						url : this.url
					}),   
					reader : reader,
					sortInfo : {field : this.defaultSortField,direction : 'ASC'},
					remoteSort : true ,
					groupField:this.myGroupField,
					baseParams: {limit:this.pagesize}
				}); 

				//返回多记录集
				this.ds.on('load',function(){
					var acl = reader.jsonData.Acl;
					if(typeof acl!="undefined"){
						var gridTb = this.getTopToolbar();
						for(var i=0;i<iLenTbar;i++){
							if(typeof this.arrTbar[i].acl != 'undefined'){
								var tbItem0 = gridTb.items.get(i);
								var tbItem1 = gridTb.items.get(i+1);

								if(this.arrTbar[i].acl == 'aNew' && acl[0].aNew===0){
									if(typeof tbItem0 != 'undefined'){tbItem0.hide();}
									if(typeof tbItem1 != 'undefined'){tbItem1.hide();}
								}

								if(this.arrTbar[i].acl == 'aEdit' && acl[0].aEdit===0){
									if(typeof tbItem0 != 'undefined'){tbItem0.hide();}
									if(typeof tbItem1 != 'undefined'){tbItem1.hide();}
								}

								if(this.arrTbar[i].acl == 'aDel' && acl[0].aDel===0){
									if(typeof tbItem0 != 'undefined'){tbItem0.hide();}
									if(typeof tbItem1 != 'undefined'){tbItem1.hide();}
								}

							}
						}

					}
				}.createDelegate(this));

				break;

			case 'array':
				reader = new Ext.data.ArrayReader({},record);

				this.ds = new Ext.data.GroupingStore({
					reader: reader,
					data: this.dataObject,
					sortInfo : {field : this.defaultSortField,direction : 'ASC'},
					groupField:this.myGroupField
				});

				break;

			default:
				break;

		}

		if(this.needGroup){
			this.view = new Ext.grid.GroupingView({
				forceFit:true,
				groupTextTpl:this.myGroupTextTpl
			});
		}

		if (this.needTbar) {
			this.tbar = this.arrTbar;
			this.getSelectionModel().on('selectionchange', function(){
				var haveselected = this.getSelectedId();
				var gridTb = this.getTopToolbar();
				for(var i=0;i<iLenTbar;i++){
					if(typeof this.arrTbar[i].acl != 'undefined'){
						var tbItem = gridTb.items.get(i);
						if(haveselected!='0'){
							if(this.arrTbar[i].acl == 'aEdit'){
								if(typeof haveselected==='number'){
									this.setBtn(tbItem,false);
								}else{
									this.setBtn(tbItem,true);
								}
							}
							if(this.arrTbar[i].acl == 'aDel' || this.arrTbar[i].acl == 'aLock'){
								this.setBtn(tbItem,false);
							}
						}else{
							if(this.arrTbar[i].acl != 'aNew'){
								this.setBtn(tbItem,true);
							}
						}
					}
				}
			}.createDelegate(this));

			if(this.needMenu){
				var onContextMenu = function(grid,rowIndex,e){
					e.stopEvent();
					grid.getSelectionModel().selectRow(rowIndex);
					var oContextMenu = new Ext.menu.Menu({
						items : this.arrMenu
					});
					oContextMenu.showAt(e.getXY());
					e.preventDefault();
				};
				this.on('rowcontextmenu',onContextMenu);
			}
		}

		if (this.needPage) {   
			var pagingToolbar = new Ext.PagingToolbar({
				store : this.ds,
				displayInfo : true,
				displayMsg : '当前显示: {0} - {1} 总记录数: {2}',
				emptyMsg : '没有符合条件的记录'
			}); 
			pagingToolbar.pageSize = this.pagesize;
			this.bbar = pagingToolbar;
		}
	},

	reload : function(){
		this.getStore().removeAll();
		this.getStore().reload({params:{start:0, limit:this.pagesize}});
	},

	setBtn : function(obj,value) {
		if(typeof obj != 'undefined'){
			obj.setDisabled(value);
		}
	},

	getSelectedId : function(){
		var selectedRow = this.getSelections();
		var selectedIds = 0,ss = 0;
		for(var i=0,len=selectedRow.length; i<len; i++){
			ss = parseInt(selectedRow[i].get(this.keyField));
			if(i==0)
				selectedIds = ss;
			else
				selectedIds = selectedIds + ',' + ss;
		}
		return selectedIds;
	}
});